#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AI营销方案生成系统 - 需求提交路由
功能：处理营销需求提交相关的API请求
作者：AI营销系统开发团队
创建时间：2024
"""

from flask import Blueprint, jsonify, request
from typing import Dict, Any
import logging

from services.requirement_service import RequirementService
from utils.response_helper import success_response, error_response
from utils.validators import validate_required_fields

# 创建蓝图
requirement_bp = Blueprint('requirements', __name__)

# 初始化服务
requirement_service = RequirementService()


@requirement_bp.route('/requirement/submit', methods=['POST'])
def submit_requirement():
    """
    提交营销需求表单
    
    Returns:
        JSON: 提交结果响应
    """
    try:
        data = request.get_json()
        
        # 验证必填字段
        required_fields = [
            'template_id',
            'core_goal',
            'target_audience', 
            'budget_per_person',
            'focus_point',
            'customer_preference',
            'customer_pain_point'
        ]
        
        validation_error = validate_required_fields(data, required_fields)
        if validation_error:
            return error_response(
                message='请求参数错误',
                error=validation_error,
                status_code=400
            )
        
        # 提交需求
        result = requirement_service.submit_requirement(
            template_id=data['template_id'],
            core_goal=data['core_goal'],
            target_audience=data['target_audience'],
            budget_per_person=data['budget_per_person'],
            focus_point=data['focus_point'],
            gift_preference=data.get('gift_preference', ''),
            specific_product=data.get('specific_product', ''),
            customer_preference=data['customer_preference'],
            customer_pain_point=data['customer_pain_point']
        )
        
        return success_response(
            data={
                'requirement_id': result['requirement_id'],
                'prompt': result['prompt']
            },
            message='需求提交成功',
            status_code=201
        )
        
    except ValueError as e:
        return error_response(
            message='需求提交失败',
            error=str(e),
            status_code=400
        )
    except Exception as e:
        logging.error(f"提交需求失败: {str(e)}")
        return error_response(
            message='需求提交失败',
            error=str(e)
        )